package com.ruoyi.web.controller.erp.inventorystatus;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.erp.componentstatus.domain.InventoryStatus;
import com.ruoyi.erp.componentstatus.mapper.InventoryStatusMapper;
import com.ruoyi.erp.componentstatus.service.InventoryStatusService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/inventoryStatuses")
public class InventoryStatusController extends BaseController {

    @Autowired
    private InventoryStatusService inventoryStatusService;





    private  String prefix = "erp/inventoryStatus";

    @RequiresPermissions("inventoryStatuses:inventoryStatus:view")
    @GetMapping("/inventoryStatus")
    public String list (){
        return prefix+"/list";
    }




    /***
     * 查询InventoryStatus全部数据
     * @return
     */
    @RequiresPermissions("inventoryStatuses:inventoryStatus:list")
    @ApiOperation(value = "查询所有inventoryStatus",notes = "查询所inventoryStatus有方法详情",tags = {"inventoryStatusController"})
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findAll(InventoryStatus inventoryStatus){
        List<InventoryStatus> list = inventoryStatusService.findAll(inventoryStatus);

        list.stream().forEach(le -> {
            le.setDemandFlag(changeFlag(le.getDemandFlag()));
            le.setOnhandFlag(changeFlag(le.getOnhandFlag()));
            le.setSupplyFlag(changeFlag(le.getSupplyFlag()));
        });

        return  getDataTable(list);
    }


    //字典转换
    private static  String changeFlag(String flag){

        if (StringUtils.isNotBlank(flag)){
            return flag.equals("Y")?"是":"否";
        }else{
            return "";
        }

    }


    /**
     * 新增
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }



    /***
     * 新增InventoryStatus数据
     * @param inventoryStatus
     * @return
     */
    @RequiresPermissions("inventoryStatuses:inventoryStatus:add")
    @ApiOperation(value = "查询所有inventoryStatus",notes = "添加inventoryStatus方法详情",tags = {"inventoryStatusController"})
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@ApiParam(name = "ScrappingCauseTab对象",value = "传入JSON数据",required = true) InventoryStatus inventoryStatus){
        inventoryStatus.setRowkey(UUID.randomUUID().toString());
        inventoryStatus.setRowversion(DateUtils.getTime());
        inventoryStatusService.add(inventoryStatus);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"添加数据成功","添加成功");
    }






    /***
     * 根据ID查询InventoryStatus数据
     * @param id
     * @return
     */
    @ApiOperation(value = "InventoryStatus根据ID查询",notes = "根据ID查询InventoryStatus方法详情",tags = {"InventoryStatusController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "String")
    @GetMapping("/findInventoryStatus/{id}")
    public String findById(@PathVariable("id") String id, ModelMap modelMap){
        List<String> ids = Arrays.asList(id.split(","));
        InventoryStatus inventoryStatus = inventoryStatusService.findById(ids).get(0);
        inventoryStatus.setSupplyFlag(changeFlag(inventoryStatus.getSupplyFlag()));
        inventoryStatus.setOnhandFlag(changeFlag(inventoryStatus.getOnhandFlag()));
        inventoryStatus.setDemandFlag(changeFlag(inventoryStatus.getDemandFlag()));
        modelMap.put("inventoryStatus",inventoryStatus);
        return prefix+"/edit";
    }




    /***
     * 修改ScrappingCauseTab数据
     * @param inventoryStatus
     *
     * @return
     */
    @RequiresPermissions("InventoryStatuses:InventoryStatus:edit")
    @ApiOperation(value = "InventoryStatus根据ID修改",notes = "根据ID修改InventoryStatus方法详情",tags = {"InventoryStatusController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "String")
    @ResponseBody
    @PostMapping("/edit")
    public AjaxResult update( @ApiParam(name = "InventoryStatus对象",value = "传入JSON数据",required = false) InventoryStatus inventoryStatus){

        inventoryStatusService.update(inventoryStatus);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"根据id修改成功","修改成功");
    }






    /***
     * 根据ID删除品牌数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "InventoryStatus根据ID删除",notes = "根据ID删除InventoryStatus方法详情",tags = {"InventoryStatusController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "String")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult delete(String ids){
        List<String> idl = Arrays.asList(ids.split(","));
        return toAjax(inventoryStatusService.delete(idl));
    }




    @Log(title = "导出交货路线", businessType = BusinessType.EXPORT)
    @RequiresPermissions("InventoryStatuses:InventoryStatus:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InventoryStatus inventoryStatus)
    {
        List<InventoryStatus> scrappingCauseTab = inventoryStatusService.findAll(inventoryStatus);
        ExcelUtil<InventoryStatus> util = new ExcelUtil<InventoryStatus>(InventoryStatus.class);
        return util.exportExcel(scrappingCauseTab, "交货路线数据");
    }







}
